﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;

namespace FYPHP
{
    public partial class AdsEdit : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SqlCommand cmdAds;
                SqlDataReader dtrAds;
                conn.Open();


                cmdAds = new SqlCommand("SELECT * FROM Advertisements WHERE a_id=@AID", conn);
                cmdAds.Parameters.AddWithValue("@AID", Request.QueryString["aid"]);
                dtrAds = cmdAds.ExecuteReader();

                if (dtrAds.Read())
                {
                    lblAID.Text = dtrAds["a_id"].ToString();
                    txtAdsLink.Text = dtrAds["a_link"].ToString().Substring(7);
                    txtAdsDuration.Text = dtrAds["a_duration"].ToString();
                    imgName.Text = dtrAds["a_picture"].ToString();
                }

                dtrAds.Close();
                conn.Close();
            }
        }

        public string getPic()
        {
            SqlCommand cmdSearch;
            SqlDataReader dtrSearch;
            conn.Open();

            cmdSearch = new SqlCommand("Select a_picture From Advertisements Where a_id=@aid", conn);
            cmdSearch.Parameters.AddWithValue("@aid", Request.QueryString["aid"]);
            dtrSearch = cmdSearch.ExecuteReader();

            dtrSearch.Read();
            String pic = dtrSearch["a_picture"].ToString();

            dtrSearch.Close();
            conn.Close();

            return pic;
        }
             
        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            string uploadFolder = Request.PhysicalApplicationPath + "/uploads/ads/";

            if (AdsFileUpload1.HasFile)
            {
                string now = String.Format("{0:ddMMyyyyHHmmss}", DateTime.Now);
                string extension = Path.GetExtension(AdsFileUpload1.PostedFile.FileName);
                AdsFileUpload1.SaveAs(uploadFolder + now + extension);
                UploadStatusLabel.Text = "File uploaded successfully as: ";
                imgName.Text = now + extension;
            }
            else
            {
                UploadStatusLabel.Text = "You did not specify a file to upload.";
                imgName.Text = "";
            }
        }

        protected void btnAdsEdit_Click(object sender, EventArgs e)
        {
            SqlCommand cmdUpdate;
            conn.Open();

            cmdUpdate = new SqlCommand("Update Advertisements SET a_link=@ALink, a_status=@AStatus, a_picture=@APicture, a_duration=@ADuration WHERE a_id=@AID", conn);
            cmdUpdate.Parameters.AddWithValue("@APicture", imgName.Text);
            cmdUpdate.Parameters.AddWithValue("@ALink", "http://" + txtAdsLink.Text);
            cmdUpdate.Parameters.AddWithValue("@AStatus", "published");
            cmdUpdate.Parameters.AddWithValue("@ADuration",txtAdsDuration.Text);
            cmdUpdate.Parameters.AddWithValue("@AID", lblAID.Text);
            int success = cmdUpdate.ExecuteNonQuery();

            if (success == 1)
            {
                MessageBox("Edit Successfully!");
                Redirect("AdsEdit.aspx?aid="+Request.QueryString["aid"].ToString());
            }
            conn.Close();
        }
    }
}